/*
* BitStreamMainData.java -- MPEG-1/MPEG-2/MPEG-2.5 Audio Layer III 主数据(main_data)位流.
* Copyright (C) 2010
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* If you would like to negotiate alternate licensing terms, you may do
* so by contacting the author: <http://jmp123.sourceforge.net/>.
*/
package jmp123.decoder;

/**
 * 用于MPEG-1/MPEG-2/MPEG-2.5 Audio Layer Ⅲ 主数据(main_data)解码时读位流并对哈夫曼数据解码。
 * @version 0.400
 */
public final class BitStreamMainData extends BitStream {
	// 用于哈夫曼解码的大值区
	private int[] lin;		// lin[0..15]=0
	private short[][] htbv;

	// 用于解码小值区
	private short[] htc0, htc1;

	/**
	 * 创建一个位流BitStreamMainData对象，位流的缓冲区大小len指定，位流的缓冲区尾部空出的长度由extra指定。
	 * @param len 缓冲区可访问长度。
	 * @param extra 缓冲区尾部空出的字节数。
	 */
	public BitStreamMainData(int len, int extra) {
		super(len, extra);
		this.initialize();
	}

	/**
	 * 一个粒度组内的一个声道哈夫曼解码。
	 * 
	 * @param region region[0..2]是解码时码表用于解码主数据区域的长度，region[3]是啥夫曼位总长度。
	 * @param table_select 解码大值区码表的索引。
	 * @param count1table_select 解码小值区(count1区)码表的索引
	 * @param hv 接收解码得到的576个值。
	 * @return 576减去零值区长度。
	 */
	public int decodeHuff(int[] region, int[] table_select, int count1table_select, int[] hv) {
		int tmp, linbits, maxidx, idx = 0;
		short[] htab;
		byte[] b = super.bitReservoir;

		/*
		 * 1. 初始化num,mask,part3len
		 * mask: 暂存位流缓冲区不超过32比特数据,位流2级缓冲
		 * num: mask剩余的比特数
		 * part3len: 哈夫曼编码的主数据(main_data)的比特数
		 */
		int part3len = region[3];
		int num = (8 - bitPos) & 7;
		int mask = 0;
		int x,y,i;

		/*
		 * 2. 使位流缓冲区按字节对齐
		 */
		if (num > 0) {
			mask = getBits9(num);
			mask <<= 32 - num;
			part3len -= num;
		}

		/*
		 * 3. 解码大值区
		 */
		for (i = 0; i < 3; i++) {
			maxidx = region[i];
			tmp = table_select[i];
			htab = htbv[tmp];
			linbits = lin[tmp];
			while (idx < maxidx) {
				if (part3len + num <= 0) { //检测位流是否有错误
					num -= part3len + num;
					break;
				}
				
				while (num < 24) { // refresh mask
					mask |= (b[bytePos++] & 0xff) << (24 - num);
					num += 8;
					part3len -= 8;
				}
				tmp = mask;
				y = htab[tmp >>> 30];
				while (y < 0) {
					tmp <<= 2;
					y = htab[(tmp >>> 30) - y];
				}
				x = y >> 8; // x暂存hlen
				num -= x;
				mask <<= x;

				x = (y >> 4) & 0xf; // 解得x,y
				y &= 0xf;

				if (x != 0) {
					if (x == 15 && linbits != 0) {
						while (num < 24) { // refresh mask
							mask |= (b[bytePos++] & 0xff) << (24 - num);
							num += 8;
							part3len -= 8;
						}
						x += mask >>> (32 - linbits); // 循环右移
						num -= linbits;
						mask <<= linbits;
					}
					hv[idx++] = (mask < 0) ? -x : x;
					num--;
					mask <<= 1;
				} else
					hv[idx++] = 0;

				if (y != 0) {
					if (y == 15 && linbits != 0) {
						while (num < 24) { // refresh mask
							mask |= (b[bytePos++] & 0xff) << (24 - num);
							num += 8;
							part3len -= 8;
						}
						y += mask >>> (32 - linbits);
						num -= linbits;
						mask <<= linbits;
					}
					hv[idx++] = (mask < 0) ? -y : y;
					num--;
					mask <<= 1;
				} else
					hv[idx++] = 0;
			}
		}

		/*
		 * 4. 解码count1区
		 */
		htab = (count1table_select == 0) ? htc0 : htc1;
		while (idx < 572) {
			while (num < 10) { // 6(max hlen) + 4(signed bit)
				mask |= (b[bytePos++] & 0xff) << (24 - num);
				num += 8;
				part3len -= 8;
			}
			tmp = mask;
			y = htab[tmp >>> 28];
			while (y < 0) {
				tmp <<= 4;
				y = htab[(tmp >>> 28) - y];
			}

			x = y >> 8; // hlen
			mask <<= x;
			num -= x;

			// 修改num后立即检测(当前粒度内的当前声道的)主数据是否处理完，使数据损坏的文件也能继续解码。
			if (part3len + num <= 0) {
				num -= part3len + num;
				break;
			}

			// 一个码字(hcod)解码得到4个值
			if ((y <<= 28) < 0) {
				hv[idx++] = mask < 0 ? -1 : 1;
				num--;
				mask <<= 1;
			} else
				hv[idx++] = 0;
			if ((y <<= 1) < 0) {
				hv[idx++] = mask < 0 ? -1 : 1;
				num--;
				mask <<= 1;
			} else
				hv[idx++] = 0;
			if ((y <<= 1) < 0) {
				hv[idx++] = mask < 0 ? -1 : 1;
				num--;
				mask <<= 1;
			} else
				hv[idx++] = 0;
			if ((y <<= 1) < 0) {
				hv[idx++] = mask < 0 ? -1 : 1;
				num--;
				mask <<= 1;
			} else
				hv[idx++] = 0;
		}

		if (num > 0) { // num位归还到位流缓冲区，也可以通过调用skipBits(-num)实现归还
			bitPos = -num; // 第6步还要使用num，借用bitPos暂存

			bytePos += bitPos >> 3;
			bitPos &= 0x7;
		}

		/*
		 * 5. rzone区直接置0,即hv[nonzero..575]=0
		 */
		int nonzero = idx;
		while (idx < 576)
			hv[idx++] = 0;

		/*
		 * 6. 丢弃附属位(ancillary_bit)。附属位不超过多少位？
		 */
		part3len += num;
		if (part3len > 0) {		// 这还不一定是附属位，码流有错误也有可能出现这种情况
			while (part3len > 9) {
				getBits9(9);	// 不再是字节对齐的
				part3len -= 9;
			}
			getBits9(part3len);
		}

		return nonzero;
	}

	//-------------------------------------------------------------------------
	private void initialize() {
		// 用于解码大值区的码表 htbvx[]: 16个数组共2752元素
		short htbv0[] = { 0, 0, 0, 0 };

		short htbv1[] = {-4, 528, 256, 256, 785, 785, 769, 769};

		short htbv2[] = {	//20
			-4, -16, 256, 256,  -8, -12, 785, 785,1570,1538,1298,1298,1313,1313,1312,1312,
			769, 769, 784, 784};

		short htbv3[] = {	//16
			-4, 529, 513, 512,  -8, -12, 784, 784,1570,1538,1298,1298,1313,1313,1312,1312};

		short htbv5[] = {	//32
			-4, -28, 256, 256,  -8, -24, 785, 785, -12,1585, -16, -20,2099,2083,1842,1842,
			1811,1811,1795,1795,1840,1840,1826,1826,1554,1569,1538,1568, 769, 769, 784, 784};

		short htbv6[] = {	//32
			-4, -24, 529, -28,  -8, -16, -20,1042, -12,1571,1586,1584,1843,1843,1795,1795,
			1299,1299,1329,1329,1314,1314,1282,1282,1057,1056, 769, 769, 784, 784, 768, 768};

		short htbv7[] = {	//72
			-4, -68, 256, 256,  -8, -48, -64,1041, -12, -28, -40, -44, -16, -20, -24,2069,
			2645,2629,2644,2643,2357,2357,2372,2372,2341,2341,2386,2386,2129, -32,2128, -36,
			2309,2309,2356,2356,2371,2371,2355,2355,2084,2114,1812,1812,1857,1857,1856,1856,
			-52, -56, -60,1554,2052,2083,2098,2051,1811,1811,1841,1841,1840,1840,1826,1826,
			1313,1313,1538,1568, 769, 769, 784, 784};

		short htbv8[] = {	//64
			-4, 529, -60, 512,  -8, -52,1042,1057, -12, -32, -44, -48, -16, -24, -28,2069,
			-20,2629,2387,2387,2901,2901,2900,2900,2613,2628,2341,2341,2386,2386,2309,2309,
			2129, -36, -40,2084,2356,2356,2371,2371,2384,2384,2355,2355,2114,2068,1857,1857,
			2052,2112,2083,2098, -56,1570,1538,1568,2067,2097,2051,2096, 769, 769, 784, 784};

		short htbv9[] = {	//72
			-4, -56, -64, -68,  -8, -36, -48, -52, -12, -24, -28, -32, -16,2101,2131, -20,
			2389,2389,2373,2373,2388,2388,2309,2309,2116,2085,2130,2069,1873,1873,1844,1844,
			1859,1859,2128,2052, -40, -44,1556,1601,1828,1828,1858,1858,1843,1843,1856,1856,
			1571,1586,1299,1299,1329,1329,1539,1584, -60,1042,1057,1056,1314,1314,1282,1282,
			785, 785, 769, 769, 784, 784, 768, 768};

		short htbv10[] = {	//124
			-4,-120, 256, 256,  -8, -88,-116,1041, -12, -48, -64, -80, -16, -32, -36, -44,
			-20, -24, -28,2631,2935,2935,2919,2919,2934,2934,2903,2903,2933,2933,2918,2918,
			2676,2646,2661,2615,2675,2630, -40,2659,2901,2901,2900,2900,2343,2343,2418,2418,
			-52, -56, -60,2071,2660,2567,2416,2416,2402,2402,2629,2613,2310,2310,2643,2628,
			2161, -68, -72, -76,2358,2358,2342,2342,2597,2642,2325,2325,2385,2385,2612,2627,
			2070,2145,2144, -84,2309,2309,2384,2384, -92,-104,-108,-112, -96,-100,2068,2113,
			2340,2340,2370,2370,2355,2355,2308,2308,2112,2083,2098,2051,1811,1811,1841,1841,
			1840,1840,1826,1826,1554,1569,1538,1568, 769, 769, 784, 784};

		short htbv11[] = {	//120
			-4,-108,-116, 512,  -8, -60, -88,-100, -12, -36, -48, -52, -16, -20, -28, -32,
			2679,2663,2678,2677,2662,2631,2676, -24,2903,2903,2901,2901,2646,2661,2359,2359,
			2419,2419,2374,2374, -40,2087,2162, -44,2629,2644,2613,2643,2404,2404,2311,2311,
			1905,1905,2071,2160,2102,2147,2144, -56,2372,2372,2341,2341, -64, -72, -76, -80,
			-68,2069,1890,1890,2386,2386,2309,2309,2086,2054,1814,1814,1889,1889,2129,2100,
			2128, -84,2084,2114,2371,2371,2355,2355, -92, -96,1555,1585,2068,2113,2052,2112,
			1827,1827,1842,1842,-104,1570,1313,1313,1795,1795,1840,1840,1042,-112, 785, 785,
			1282,1282,1312,1312, 769, 769, 784, 784};

		short htbv12[] = {	//128
			-4,-100,-116,-124,  -8, -52, -76, -96, -12, -32, -40, -44, -16, -20, -24, -28,
			2679,2663,2422,2422,2391,2391,2421,2421,2406,2406,2375,2375,2420,2420,2405,2405,
			2134,2103, -36,2087,2419,2419,2389,2389,2162,2118,2148,2071,2161, -48,2102,2147,
			2311,2311,2416,2416, -56, -64, -68, -72,2117,2132,2116, -60,2310,2310,2309,2309,
			1830,1830,1890,1890,1889,1889,2070,2144,2101,2131,2085,2130, -80, -84, -88, -92,
			1813,1813,1873,1873,1844,1844,1859,1859,2128,2052,1828,1828,1858,1858,1812,1812,
			1587,1601,1571,1586,-104,-112,1042,1057,-108,1584,1299,1299,1856,1856,1795,1795,
			1329,1329,1314,1314,-120,1024, 785, 785,1282,1282,1312,1312, 769, 769, 784, 784};

		short htbv13[] = {	//512
			-4,-508, 256, 256,  -8,-416,-484,-504, -12,-276,-340,-392, -16,-160,-220,-256,
			-20,-104,-128,-148, -24, -56, -76, -92, -28, -40, -44, -52, -32,4351,4335,4319,
			-36,4861,4589,4589,5118,5118,5116,5116,4334,4303,4318,4287,4347,4302,4316, -48,
			4527,4527,4585,4585,4076,4076,4061,4061, -60, -64, -68, -72,4346,4301,4030,4030,
			4075,4075,3999,3999,4089,4089,4074,4074,4029,4029,4059,4059, -80, -84, -88,3831,
			3983,3983,4088,4088,4044,4044,4270,4254,3982,3982,4223,4222,3802, -96,-100,3695,
			4013,4013,4028,4028,4043,4043,4086,4086,-108,-112,-116,-124,3816,3679,3741,3801,
			3829,3815,3756,3771,3663,3828,-120,3827,4042,4042,4070,4070,3391,3391,3725,3800,
			-132,-136,-140,-144,3375,3375,3570,3570,3694,3740,3343,3343,3785,3678,3499,3499,
			3709,3799,3406,3406,-152,-156,3103,3313,3784,3798,3390,3390,3513,3513,3739,3754,
			-164,-180,-188,-208,3312,-168,-172,-176,3514,3514,3557,3557,3556,3556,3468,3468,
			3437,3437,3555,3555,3298,-184,3102,3297,3374,3374,3342,3342,-192,-196,-200,-204,
			3552,3552,3421,3421,3541,3541,3452,3452,3527,3527,3405,3405,3467,3467,3512,3512,
			-212,-216,3270,3133,3540,3540,3482,3482,3497,3497,3436,3436,-224,-232,-244,-252,
			-228,3117,3282,3101,3539,3539,3451,3451,3255,-236,-240,3267,3420,3420,3525,3525,
			3481,3481,3450,3450,-248,3147,3025,3025,3495,3495,3479,3479,3085,3280,3210,3240,
			-260,-264,-268,-272,3148,3268,3179,3254,2876,2876,2860,2860,3010,3010,2907,2907,
			3253,3209,2844,2844,-280,-300,-316,-328,-284,-288,-292,-296,3009,3009,3224,3084,
			3008,3008,3252,3178,3238,3193,2875,2875,2995,2995,3208,3162,-304,-308,-312,2738,
			2859,2859,3237,3177,2980,2980,3192,3207,2964,2964,3191,3190,2587,2737,-320,-324,
			2827,2827,2992,2992,2966,2966,2890,2890,-332,-336,2602,2722,2874,2874,2979,2979,
			2905,2905,2965,2965,-344,-352,-368,-376,2586,2721,-348,2720,2826,2826,2920,2920,
			-356,2707,-360,-364,2950,2950,2889,2889,2873,2873,2904,2904,2949,2949,2919,2919,
			2601,2706,-372,2616,2903,2903,2933,2933,2691,-380,-384,-388,2918,2918,2887,2887,
			2932,2932,2902,2902,2917,2917,2931,2931,-396,-400,-404,-412,2329,2329,2449,2449,
			2569,2704,2632,2692,2674,-408,2344,2344,2886,2886,2916,2916,2434,2434,2328,2328,
			-420,-440,-456,-468,-424,-428,-432,-436,2615,2599,2327,2327,2417,2417,2645,2567,
			2672,2614,2659,2629,2644,2598,2658,2613,2177,-444,-448,-452,2312,2312,2432,2432,
			2326,2326,2401,2401,2310,2310,2400,2400,-460,-464,2069,2129,2643,2628,2341,2341,
			2386,2386,2309,2309,-472,-476,-480,2068,2356,2356,2371,2371,2384,2384,2340,2340,
			2370,2370,2355,2355,-488,-492,-496,-500,1857,1857,2052,2112,2083,2098,1811,1811,
			1841,1841,1795,1795,1840,1840,1826,1826,1554,1569,1538,1568,1041,1025, 784, 784};

		short htbv15[] = {	//528
			-4,-460,-512,-524,  -8,-284,-368,-432, -12,-124,-208,-256, -16, -60, -84,-104,
			-20, -36, -52, -56, -24, -28,3310, -32,3583,3583,3567,3567,3582,3582,3551,3551,
			3581,3581,3535,3535, -40, -44,3323, -48,3580,3580,3550,3550,3565,3565,3519,3519,
			3534,3534,3564,3564,3293,3247,3322,3262,3307,3277,3292,3231, -64, -68, -72, -76,
			3321,3306,3261,3291,3215,3320,3276,3230,3305,3199,3319,3245,3290,3260,3183, -80,
			3502,3502,3343,3343, -88, -92, -96,-100,3019,3019,3062,3062,3214,3304,3167,3229,
			3061,3061,2942,2942,3047,3047,2988,2988,-108,-112,-116,-120,3018,3018,3003,3003,
			3289,3213,2895,2895,3060,3060,2879,2879,3059,3059,3032,3032,-128,-148,-168,-188,
			-132,-136,-140,-144,3046,3046,2863,2863,3058,3058,3182,3312,2847,2847,3057,3057,
			2972,2972,3017,3017,-152,-156,-160,-164,2910,2910,2987,2987,3002,3002,3045,3045,
			2941,2941,3031,3031,2894,2894,3044,3044,-172,-176,-180,-184,2956,2956,3016,3016,
			2878,2878,2925,2925,3030,3030,3043,3043,2971,2971,3001,3001,-192,-196,-200,-204,
			2862,2862,2986,2986,3042,3042,2846,2846,3041,3041,3086,3296,2909,2909,3029,3029,
			-212,-228,-240,-248,-216,-220,2772,-224,2940,2940,3015,3015,2893,2893,2955,2955,
			3000,3000,2970,2970,-232,-236,2771,2770,2985,2985,2924,2924,3014,3014,2877,2877,
			-244,2589,2683,2743,2861,2861,2829,2829,2769,-252,2757,2698,2908,2908,3024,3024,
			-260,-264,-272,-280,2728,2636,2756,2667,2742,-268,2620,2755,2969,2969,2828,2828,
			2682,2727,2726,-276,3008,3008,2827,2827,2498,2498,2604,2651,-288,-308,-328,-348,
			-292,-296,-300,-304,2741,2588,2697,2712,2753,2635,2740,2666,2619,2681,2483,2483,
			2711,2696,2603,2650,-312,-316,-320,-324,2482,2482,2725,2587,2481,2481,2736,2665,
			2710,2634,2724,2680,2695,2618,2467,2467,-332,-336,-340,-344,2393,2393,2453,2453,
			2346,2346,2466,2466,2330,2330,2465,2465,2570,2720,2408,2408,-352,-356,-360,-364,
			2438,2438,2377,2377,2452,2452,2361,2361,2451,2451,2679,2569,2392,2392,2437,2437,
			-372,-388,-408,-412,-376,-380,2193,-384,2345,2345,2407,2407,2422,2422,2450,2450,
			2329,2329,2448,2448,-392,-396,-400,-404,2376,2376,2436,2436,2391,2391,2421,2421,
			2360,2360,2435,2435,2406,2406,2375,2375,2088,2178,2072,2177,-416,-420,-424,-428,
			2420,2420,2312,2312,2432,2432,2390,2390,2405,2405,2359,2359,2419,2419,2374,2374,
			-436,-440,-448,-452,2087,2162,2148,2071,2133,2161,-444,2102,2311,2311,2416,2416,
			2147,2117,2132,2086,2146,2070,-456,2101,2310,2310,2400,2400,-464,-484,-500,-508,
			-468,-472,-476,-480,1889,1889,2131,2116,1829,1829,1874,1874,1813,1813,1873,1873,
			2053,2128,1844,1844,-488,-492,1601,-496,1859,1859,1828,1828,1858,1858,1843,1843,
			1812,1812,1796,1796,1571,1586,-504,1555,1856,1856,1795,1795,1585,1584,1314,1314,
			-516,-520, 785, 785,1298,1298,1313,1313,1282,1282,1312,1312,1025,1040, 768, 768};

		short htbv16[] = {	//512
			-4,-508, 256, 256,  -8,-328,-468,-504, -12, -52,-120,-208, -16, -36, -48,2303,
			-20, -24, -28, -32,3055,3055,3070,3070,3039,3039,3069,3069,3023,3023,3068,3068,
			3007,3007,3067,3067,2735, -40, -44,2703,3066,3066,2975,2975,3065,3065,3064,3064,
			2687,2807,2671,2806, -56, -60, -64,2290,2655,2805,2383,2383,2548,2548,2547,2547,
			2544,2544,2623, -68, -72, -92,-100,-112, -76, -84,3822, -88,4302, -80,4062,4062,
			4588,4588,4573,4573,4073,4073,4330,4313,4077,4077,4075,4075,3774,3789, -96,3758,
			4060,4060,4059,4059,3788,-104,-108,3786,4013,4013,4058,4058,3966,3966,4012,4012,
			-116,3678,3517,3517,4041,4041,3965,3965,-124,2079,2289,-128,2351,2351,2319,2319,
			-132,-152,-172,-188,-136,-140,-144,-148,3486,3486,3772,3787,3726,3816,3741,3815,
			3771,3725,3800,3694,3558,3558,3484,3484,-156,-160,-164,-168,3755,3770,3813,3799,
			3406,3406,3812,3724,3528,3528,3390,3390,3437,3437,3798,3739,-176,-180,-184,3299,
			3769,3754,3553,3553,3540,3540,3768,3753,3451,3451,3767,3792,-192,-196,-200,-204,
			3342,3342,3552,3552,3421,3421,3541,3541,3452,3452,3527,3527,3405,3405,3467,3467,
			-212,-264,-288,-308,-216,-232,-248,-260,-220,-224,-228,3085,3482,3482,3436,3436,
			3526,3526,3389,3389,3420,3420,3525,3525,-236,-240,-244,3132,3466,3466,3496,3496,
			3481,3481,3404,3404,3510,3510,3450,3450,-252,3100,3264,-256,3419,3419,3465,3465,
			3480,3480,3449,3449,3042,3042,3118,3102,-268,-272,-280,-284,3283,3117,3282,3281,
			3131,-276,2845,2845,3479,3479,3464,3464,3268,3179,3267,3239,2860,2860,3266,3253,
			-292,-296,-300,-304,3265,3084,3147,3252,3178,3238,2995,2995,3162,3237,2859,2859,
			2994,2994,2843,2843,-312,-316,-320,-324,2993,2993,3083,3248,3177,3222,3146,3236,
			3192,3207,2979,2979,3130,3161,2858,2858,-332,-392,-432,-452,-336,-352,-368,-376,
			-340,-344,-348,2722,3221,3176,2977,2977,3206,3191,2964,2964,3145,3159,2919,2919,
			2586,-356,-360,-364,2826,2826,2976,2976,2873,2873,2963,2963,2904,2904,2949,2949,
			2601,2706,-372,2585,2934,2934,2825,2825,2705,-380,-384,-388,2960,2960,2888,2888,
			2948,2948,2933,2933,2872,2872,2947,2947,-396,-408,-416,-424,-400,2690,-404,2584,
			2918,2918,2856,2856,2887,2887,2932,2932,2689,2688,-412,2615,2824,2824,2902,2902,
			2675,-420,2599,2674,2917,2917,2886,2886,-428,2567,2327,2327,2916,2916,2901,2901,
			-436,-440,-444,-448,2417,2417,2672,2614,2659,2629,2644,2598,2402,2402,2326,2326,
			2401,2401,2566,2656,-456,-460,2129,-464,2387,2387,2613,2628,2341,2341,2386,2386,
			2325,2325,2309,2309,-472,-488,-496,-500,-476,-480,-484,2068,2356,2356,2371,2371,
			2384,2384,2340,2340,2370,2370,2355,2355,2113,-492,2083,2098,2308,2308,2368,2368,
			1811,1811,1841,1841,2051,2096,1826,1826,1554,1569,1538,1568,1041,1025, 784, 784};

		short htbv24[] = {	//508
			-4,-140,-452,-504,  -8, -28, -48,1279, -12, -16, -20, -24,2287,2302,2271,2301,
			2255,2300,2239,2299,2042,2042,2223,2207,2041,2041,2040,2040, -32, -36, -40, -44,
			2191,2175,2039,2039,1903,1903,2038,2038,1887,1887,2037,2037,1871,1871,2036,2036,
			-52, -56, -60, -64,1855,1855,2035,2035,1839,1839,2034,2034,2033,2033,2079,2288,
			-68, -80,-100,-120,2319,2319, -72, -76,3054,3054,3038,3038,3053,3053,3022,3022,
			-84, -88, -92, -96,3052,3052,3037,3037,3006,3006,3051,3051,3021,3021,3036,3036,
			2990,2990,3050,3050,-104,-108,-112,-116,3005,3005,3035,3035,3020,3020,2974,2974,
			3049,3049,2989,2989,3034,3034,3004,3004,-124,-128,-132,-136,3019,3019,2958,2958,
			3048,3048,2973,2973,3033,3033,2942,2942,3047,3047,2988,2988,-144,-256,-352,-428,
			-148,-192,-212,-232,-152,-168,-176,-184,-156,-160,-164,2790,3018,3018,3003,3003,
			2957,2957,3032,3032,3086,3296,2829,2829,-172,2761,2654,2746,2926,2926,2972,2972,
			2789,-180,2775,2788,2987,2987,2941,2941,2700,2760,-188,2622,2894,2894,2862,2862,
			-196,-200,-204,-208,2669,2774,2787,2715,2745,2730,2786,2590,2785,2653,2773,2684,
			2759,2637,2699,2744,-216,-220,-224,-228,2772,2714,2729,2668,2758,2621,2771,2605,
			2770,2589,2683,2743,2769,2652,2757,2698,-236,-240,-248,-252,2728,2713,2636,2756,
			2667,2742,-244,2620,3024,3024,2828,2828,2755,2682,2727,2604,2754,2651,2741,2588,
			-260,-292,-312,-332,-264,-268,-280,-284,2697,2712,2753,2635,-272,2619,-276,2586,
			3008,3008,2827,2827,2992,2992,2826,2826,2484,2484,2666,2726,2681,2711,-288,2704,
			2976,2976,2825,2825,-296,-300,-304,-308,2483,2483,2440,2440,2603,2650,2482,2482,
			2725,2587,2737,2665,2454,2454,2468,2468,-316,-320,-324,-328,2634,2680,2439,2439,
			2362,2362,2467,2467,2393,2393,2453,2453,2346,2346,2466,2466,-336,-340,-344,-348,
			2465,2465,2408,2408,2438,2438,2423,2423,2377,2377,2452,2452,2361,2361,2451,2451,
			-356,-376,-396,-416,-360,-364,-368,-372,2392,2392,2437,2437,2345,2345,2407,2407,
			2422,2422,2450,2450,2329,2329,2449,2449,-380,-384,-388,-392,2376,2376,2436,2436,
			2391,2391,2421,2421,2360,2360,2435,2435,2406,2406,2344,2344,-400,-404,-408,-412,
			2434,2434,2328,2328,2375,2375,2420,2420,2433,2433,2568,2688,2390,2390,2405,2405,
			-420,2163,-424,2162,2327,2327,2567,2672,2359,2359,2343,2343,-432,-436,-440,-444,
			2118,2148,2133,2161,2102,2147,2117,2132,2086,2146,2070,2145,-448,2101,2131,2116,
			2310,2310,2400,2400,-456,-480,-492,-500,-460,-468,-472,-476,2085,2130,2069,-464,
			2309,2309,2384,2384,1873,1873,2100,2115,1828,1828,1858,1858,1843,1843,1812,1812,
			-484,-488,1555,1585,1857,1857,2052,2112,1827,1827,1842,1842,-496,1570,1298,1298,
			1795,1795,1840,1840,1313,1313,1538,1568,1041,1025,1040,1024};

		// 用于解码小值区(count1 区)的码表 htcx[]: 2数组共96元素
		htc0 = new short[] {	//80
			-16, -32, -48, -64,1026,1025,1028,1032, 256, 256, 256, 256, 256, 256, 256, 256,
			1547,1547,1547,1547,1551,1551,1551,1551,1549,1549,1549,1549,1550,1550,1550,1550,
			1543,1543,1543,1543,1541,1541,1541,1541,1289,1289,1289,1289,1289,1289,1289,1289,
			1286,1286,1286,1286,1286,1286,1286,1286,1283,1283,1283,1283,1283,1283,1283,1283,
			1290,1290,1290,1290,1290,1290,1290,1290,1292,1292,1292,1292,1292,1292,1292,1292};

		htc1 = new short[] {	//16
			1039,1038,1037,1036,1035,1034,1033,1032,1031,1030,1029,1028,1027,1026,1025,1024};

		lin = new int[32];
		htbv = new short[32][];
		htbv[0] = htbv0; // hlen=0
		htbv[1] = htbv1;
		htbv[2] = htbv2;
		htbv[3] = htbv3;
		htbv[4] = htbv0; // not used
		htbv[5] = htbv5;
		htbv[6] = htbv6;
		htbv[7] = htbv7;
		htbv[8] = htbv8;
		htbv[9] = htbv9;
		htbv[10] = htbv10;
		htbv[11] = htbv11;
		htbv[12] = htbv12;
		htbv[13] = htbv13;
		htbv[14] = htbv0; // not used
		htbv[15] = htbv15;
		htbv[16] = htbv[17] = htbv[18] = htbv[19] = htbv[20] = htbv[21] = htbv[22] = htbv[23] = htbv16;
		htbv[24] = htbv[25] = htbv[26] = htbv[27] = htbv[28] = htbv[29] = htbv[30] = htbv[31] = htbv24;
		// lin[0..15] = 0
		lin[16] = 1;
		lin[17] = 2;
		lin[18] = 3;
		lin[19] = 4;
		lin[20] = 6;
		lin[21] = 8;
		lin[22] = 10;
		lin[23] = 13;
		lin[24] = 4;
		lin[25] = 5;
		lin[26] = 6;
		lin[27] = 7;
		lin[28] = 8;
		lin[29] = 9;
		lin[30] = 11;
		lin[31] = 13;
	}
}